/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package espe.ccsoft.controllers;

import espe.ccsoft.controllers.utils.JsfUtil;
import espe.ccsoft.dao.AccionRolDao;
import espe.ccsoft.models.Accion;
import espe.ccsoft.models.AccionRol;
import java.util.List;
import java.util.ResourceBundle;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class AccionRolController {
    private AccionRol accionrol;
    private AccionRolDao accionroldao;
    
    public AccionRol getSelected() {
        if (accionrol == null) {
            accionrol = new AccionRol();
        }

        return accionrol;
    }

    public AccionRolDao getAccionRolDao() {
        accionroldao = new AccionRolDao();
        return accionroldao;
    }
    
    public AccionRol findAByIdRolIdAccion(int id_rol, int id) throws Exception{
        return getAccionRolDao().findAByIdRolIdAccion(id_rol, id);
    }
    
    public List<AccionRol> findAllByIdRolIdAccion(final int id_rol, final int id_accion) throws Exception{
        return getAccionRolDao().findallByRolIdAccionId(id_rol, id_accion);
    }
    
    public List<AccionRol> findAllByIdRolIdPermiso(final int id_rol, final int id_permiso) throws Exception{
        return getAccionRolDao().findAllByPermisoRol(id_rol, id_permiso);
    }
    
    public void crearAcciones(List<Integer> id_accion, int id_rol){
        try {
            for (Object idaccion : id_accion) {
                AccionRol accionrolTmp = new AccionRol();

                accionrolTmp.setAccionrol(Integer.parseInt(idaccion.toString()), id_rol);
                getAccionRolDao().insert(accionrolTmp);

            }
        } catch (Exception e) {
            JsfUtil.addErrorMessage(ResourceBundle.getBundle("/PermisosLabels").getString("Error"));
        }
    }
    
    public void elminarAcciones(int id_rol){
        try{
            getAccionRolDao().delete(id_rol);
        }catch (Exception e) {
            JsfUtil.addErrorMessage(ResourceBundle.getBundle("/PermisosLabels").getString("Error"));
        }
    }
}
